package com.dashu.park.controller;

import com.dashu.base.enums.ReplRyCode;
import com.dashu.park.dto.PageDTO;
import com.dashu.park.dto.company.EnterpriseDTO;
import com.dashu.park.result.PageEnty;
import com.dashu.park.result.ResultBean;
import com.dashu.park.service.EnterpriseService;
import com.dashu.park.vo.UpdateEntityVO;
import com.dashu.park.vo.company.EnterpriseListVO;
import com.dashu.park.vo.company.EnterpriseVO;
import com.dashu.park.vo.company.UpEnterpriseVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * @Author xys
 * @Date 2020/ReplRyCode.SUCCESS.getCode()ReplRyCode.SUCCESS.getCode()/ReplRyCode.SUCCESS.getCode()7 ReplRyCode.SUCCESS.getCode()5:2ReplRyCode.SUCCESS.getCode()
 */

@RequestMapping("dashu/enterprise")
@RestController
@Slf4j
@Api(value = "dashu/enterprise", tags = {"企业模块"}, description = "后台")
public class EnterpriseController {

    @Autowired
    private EnterpriseService enterpriseService;

    @PostMapping("/addEnterprise")
    @ApiOperation(value = "企业添加", notes = "企业添加")
    public ResultBean<EnterpriseDTO> addEnterprise(@RequestBody @Validated EnterpriseVO enterpriseVO) {
        EnterpriseDTO enterpriseDTO = enterpriseService.existByCompanyName(enterpriseVO.getEnterpriseName(),0);
        enterpriseService.isAdd(enterpriseVO);
        if (enterpriseDTO != null)
            return new ResultBean<>(ReplRyCode.EXIST_COMPANY.getCode(), ReplRyCode.EXIST_COMPANY.getMessage(), null);

        if (enterpriseVO == null) {
            return new ResultBean<>(ReplRyCode.DATA_NOT_NULL.getCode(), ReplRyCode.DATA_NOT_NULL.getMessage(), null);
        }
        return new ResultBean<>(ReplRyCode.SUCCESS.getCode(), "企业添加", enterpriseService.add(enterpriseVO));
    }

    @PostMapping("/updataEnterprise")
    @ApiOperation(value = "企业信息修改", notes = "企业信息修改")
    public ResultBean<EnterpriseDTO> updataPass(@RequestBody @Validated UpdateEntityVO<UpEnterpriseVO> updateEnterpriseVO) throws Exception {
        if (updateEnterpriseVO == null) {
            return new ResultBean<>(ReplRyCode.DATA_NOT_NULL.getCode(), ReplRyCode.DATA_NOT_NULL.getMessage(), null);
        }
        return new ResultBean<>(ReplRyCode.SUCCESS.getCode(), "企业信息修改", enterpriseService.updataByEnterpriseId(updateEnterpriseVO));

    }

    @PostMapping("/findBase")
    @ApiOperation(value = "父节点查询部门列表", notes = "父节点查询部门列表")
    public ResultBean<List<EnterpriseDTO>> findBase(@RequestBody @Validated PageEnty<Long> creteUserVo) {
        if (creteUserVo == null) {
            return new ResultBean<>(ReplRyCode.DATA_NOT_NULL.getCode(), ReplRyCode.DATA_NOT_NULL.getMessage(), null);
        }
        return new ResultBean<>(ReplRyCode.SUCCESS.getCode(), "父节点查询部门列表", enterpriseService.findByCreteUser(creteUserVo));

    }

    @PostMapping("/findEnterprise")
    @ApiOperation(value = "企业信息查询", notes = "企业信息查询")
    public ResultBean<EnterpriseDTO> findEnterprise(long enterpriseId) {
        if (enterpriseId == 0) {
            return new ResultBean<>(ReplRyCode.DATA_NOT_NULL.getCode(), ReplRyCode.DATA_NOT_NULL.getMessage(), null);
        }
        return new ResultBean<>(ReplRyCode.SUCCESS.getCode(), "子用户查询", enterpriseService.findByEnterpriseId(enterpriseId));
    }

    @PostMapping("/findEnterpriseName")
    @ApiOperation(value = "企业名称信息查询(模糊搜索)", notes = "企业名称信息查询")
    public ResultBean<List<EnterpriseDTO>> findEnterpriseName(String enterpriseName, long baseId) {
        if (enterpriseName == null) {
            return new ResultBean<>(ReplRyCode.DATA_NOT_NULL.getCode(), ReplRyCode.DATA_NOT_NULL.getMessage(), null);
        }
        return new ResultBean<>(ReplRyCode.SUCCESS.getCode(), "企业名称信息查询", enterpriseService.findByEnterpriseName(enterpriseName, baseId));
    }

    @DeleteMapping("/deleteEnterprise")
    @ApiOperation(value = "删除企业信息", notes = "删除企业信息")
    public ResultBean<Boolean> deleteEnterprise(long enterpriseDepartmentId) throws Exception {
        if (enterpriseDepartmentId == 0) {
            return new ResultBean<>(ReplRyCode.DATA_NOT_NULL.getCode(), ReplRyCode.DATA_NOT_NULL.getMessage(), null);
        }
        return new ResultBean<>(ReplRyCode.SUCCESS.getCode(), "删除企业信息", enterpriseService.deleteByEnterpriseId(enterpriseDepartmentId));
    }

    @PostMapping("/pageEnterpriseList")
    @ApiOperation(value = "企业信息列表", notes = "企业信息列表")
    public ResultBean<PageDTO<EnterpriseDTO>> pageEnterpriseList(@RequestBody @Validated PageEnty<EnterpriseListVO> enterpriseListPageEntyVO) throws Exception {
        PageDTO<EnterpriseDTO> pageDTO = new PageDTO<>();
        if (enterpriseListPageEntyVO == null) {
            return new ResultBean<>(ReplRyCode.DATA_NOT_NULL.getCode(), ReplRyCode.DATA_NOT_NULL.getMessage(), null);
        }
        long total = enterpriseService.enterpriseCount(enterpriseListPageEntyVO);
        pageDTO.setTotal(total);
        pageDTO.setDataList(enterpriseService.pageEnterpriseList(enterpriseListPageEntyVO));
        return new ResultBean<>(ReplRyCode.SUCCESS.getCode(), "企业信息列表", pageDTO);
    }




}
